package com.wuliu.controller;

import cn.hutool.core.util.IdUtil;
import com.wuliu.common.annotation.NoAuth;
import com.wuliu.common.core.domain.ApiResult;
import com.wuliu.common.enums.RedisDelayQueueEnum;
import com.wuliu.common.utils.RedissonUtils;
import com.wuliu.system.service.FileUploadService;
import com.wuliu.system.service.JPushClientService;
import com.wuliu.system.service.CommonService;
import com.wuliu.system.service.rabbitmq.RabbitMQService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

/**
 * @author realue
 * @version 1.0
 * @date 2023-10-12 15:49
 */
@RestController
@RequestMapping("/app/test")
@Api(tags = {"测试相关控制器"})
public class TestController {
    @Resource
    private FileUploadService fileUploadService;
    @Resource
    private CommonService commonService;
    @Resource
    private RedissonUtils redissonUtils;
    @Resource
    private RabbitMQService rabbitMQService;

    @PostMapping("/uploadFile")
    @ApiOperation(value = "上传文件", notes = "请求格式:multipart/form-data")
    @ApiImplicitParam(name = "file", value = "上传文件", required = true, dataType = "MultipartFile", dataTypeClass = MultipartFile.class, paramType = "query")
    public ApiResult<String> uploadFile(@RequestPart("file") MultipartFile file) {
        return ApiResult.success(fileUploadService.uploadFile(file, ""));
    }

    @PostMapping("/getOrderNo")
    @ApiOperation(value = "获取单号")
    public ApiResult<List<String>> getOrderNo() {
        JPushClientService jPushClientService = new JPushClientService("wuliu");
        Boolean bool = jPushClientService.sendToAlias(Arrays.asList("13543492539"), "测试", "测试", "测试", "测试", "测试");
        return ApiResult.success(commonService.createOrderNo("wuliu", 1, 10));
    }

    @NoAuth
    @ApiOperation(value = "添加延时队列")
    @PostMapping("/addQueue")
    public ApiResult<Boolean> addQueue() {
        Map<String, String> map1 = new HashMap<>();
        map1.put("orderId", "acf160e3-7c82-475f-a2ff-d204e4697bf7");

        // 添加订单支付超时，自动取消订单延迟队列。为了测试效果，延迟10秒钟
        redissonUtils.addDelayQueue(map1, RedisDelayQueueEnum.ORDER_COLLECT_TIMEOUT.getQueueName());
        return ApiResult.success(Boolean.TRUE);
    }

    @NoAuth
    @ApiOperation(value = "添加rabbitmq延时队列")
    @PostMapping("/addQueue2")
    public ApiResult<Boolean> addQueue2(Integer delayTime) {
        rabbitMQService.sendOrderDelay(IdUtil.simpleUUID(),delayTime);
        return ApiResult.success();
    }
}
